﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using myMvcBlog.Models;
using myMvcBlog.Infrastructure;

namespace myMvcBlog.Controllers
{
    public class TagController : BaseController
    {
        //
        // GET: /Tag/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult View(string nicename, int? page)
        {
            var posts = (from p in Post.All()
                         join pt in PostTag.All() on p.ID equals pt.PostID
                         join t in Tag.All() on pt.TagID equals t.ID
                         where t.UrlName == nicename && p.IsPublished
                         select p).OrderByDescending(p => p.PostedTime);

            ListView<PostView> lvTag = new ListView<PostView>();
            lvTag.Title = string.Format("标签：{0}", nicename);

            var ps = posts.Skip(page ?? 0 * ItemsPerPage).Take(ItemsPerPage);


            lvTag.List = ps.ToPostViews();

            lvTag.Pager = new PagerView()
            {
                ControllerName = "Tag",
                ActionName = "View",
                CurrentPage = page ?? 0,
                PageCount = (int)Math.Ceiling((double)posts.Count() / ItemsPerPage)
            };

            return View(lvTag);
        }

        
    }
}
